import datetime

from django.db import models


# 商品分类
from users.models import UserInfo


class Category(models.Model):
    """商品分类"""
    IS_SHOW = (
        (1, '有效'),
        (0, '无效'),
    )
    name = models.CharField(verbose_name='分类名称', max_length=64)
    parent = models.ForeignKey('self', verbose_name='父项', default=None, blank=True, null=True,  on_delete=models.CASCADE)
    link_url = models.CharField(verbose_name="链接地址", max_length=120, blank=True, null=True)
    sort = models.IntegerField(verbose_name='顺序', blank=True, null=True)
    is_show = models.IntegerField(verbose_name='是否有效', default=1, choices=IS_SHOW)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    class Meta:
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name
        ordering = ['sort']

    def __str__(self):
        return self.name


class Goods(models.Model):
    """商品"""
    DROP = (
        (0, '否'),
        (1, '下架'),
    )
    sn = models.CharField(verbose_name='商品货号', max_length=24, default=datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
    name = models.CharField(verbose_name='商品名称', max_length=150)
    desc = models.CharField(verbose_name='商品描述', max_length=256, blank=True, null=True)
    category = models.ForeignKey(Category, verbose_name='商品分类', on_delete=models.SET_NULL, blank=True, null=True, db_constraint=False)
    price = models.DecimalField(verbose_name='销售价格', max_digits=9, decimal_places=2, null=True, blank=True, default=0)
    content = models.TextField(verbose_name='产品详情', null=True, blank=True)
    stock_nums = models.IntegerField(verbose_name='库存量', default=99, null=True, blank=True)
    is_new = models.BooleanField(default=False, verbose_name="是否新品", null=True, blank=True)
    is_hot = models.BooleanField(default=False, verbose_name="是否热销", null=True, blank=True)
    click_nums = models.IntegerField(verbose_name='点击量', default=0, null=True, blank=True)
    drop = models.IntegerField(verbose_name='下架', null=True, blank=True, choices=DROP)
    sort = models.IntegerField(verbose_name='顺序', blank=True, null=True)
    user = models.ForeignKey(UserInfo, verbose_name='操作人', on_delete=models.DO_NOTHING, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '商品'
        verbose_name_plural = verbose_name
        ordering = ["-id"]


class GoodsImage(models.Model):
    """商品图片"""
    goods = models.ForeignKey(Goods, verbose_name='商品', related_name="images", on_delete=models.CASCADE, blank=True, null=True, db_constraint=False)
    name = models.CharField(verbose_name='图片名称', max_length=128, blank=True, null=True)
    image = models.ImageField(verbose_name='图片', upload_to='goods/', blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '商品图片'
        verbose_name_plural = verbose_name


class GiftPackage(models.Model):
    """礼包"""
    card_no = models.CharField(verbose_name='卡号', max_length=32)
    gift_name = models.CharField(verbose_name='礼包名称', max_length=64, blank=True, null=True)
    goods = models.ManyToManyField(to=Goods, verbose_name='礼包商品', blank=True)
    comment = models.CharField(verbose_name='备注', max_length=128, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.gift_name

    class Meta:
        verbose_name = '礼包'
        verbose_name_plural = verbose_name
        ordering = ["-id"]


